<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.getFiles.php');

if (reqVar('azione')=='elimina') {
	$msgPag .= azioneElimina();
}
if (reqVar('azione')=='carica') {
	$msgPag .= azioneCarica();
}
if (reqVar('azione')=='installa') {
	$msgPag .= azioneInstalla();
}

$sql_sel = "SELECT m.cod_modulo, m.descr as m_descr "
		.", r.* "
		." FROM wfm_regole r " 
		." LEFT JOIN wfm_moduli m ON r.id_modulo = m.id_modulo "
		." WHERE r.id_regola = ".reqVarInt('id');
$objDB->query($sql_sel,'selRec');
if(!$aRow = $objDB->fetch('selRec')) {
	backOnErr("id. errato");
}
reqDef('tipo_regola',$aRow['tipo_regola']);
reqDef('cod_regola',$aRow['cod_regola']);
reqDef('descr',$aRow['descr']);
reqDef('script',$aRow['script']);
reqDef('idm',$aRow['id_modulo']);

$aCamForm = array();
require("_inc/const.campiReg.php");

$sCmdL = "";
$sCmdL .= "<a class=\"cmd1\" href=\"regScar.php?azione=scarica&id=".reqVarUE('id')."&$sXSNQS"."\" >scarica</a>";
$sCmdL .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdL .= "<a class=\"cmd1\" href=\"regScar.php?azione=scarinst&id=".reqVarUE('id')."&$sXSNQS"."\" >scarica installato</a>";
$sCmdL .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdL .= "<a class=\"cmd1\" href=\"javascript:confInst();\" >installa</a>";
$sCmdL .= "&nbsp;&nbsp;&nbsp;&nbsp;";

$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:confElim();\" >elimina</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class=\"cmd1\" href=\"regMod.php?id=".reqVarUE('id')."&$sXSNQS"."\" >modifica</a>";
$sCmdR .= cmdTorna('torna');

$sHtmlMod = "<table class=\"tab1\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"100%\" align='center'>\n";
$sHtmlMod .= getHtmlRigaRif();
$sHtmlMod .= "<tr>\n";
$sHtmlMod .= "<td class='lab1' colspan='3'>Modulo</td>\n";
$sHtmlMod .= "<td class='txt1' colspan='3'>".$aRow['cod_modulo']."</td>\n";
$sHtmlMod .= "<td class='txt1' colspan='14'>".$aRow['m_descr']."</td>\n";
$sHtmlMod .= "</tr>\n";
$sHtmlMod .= "</table>";

$aOpz['sottotit'] = $sHtmlMod;

testaHtml('Gestione work-flow','Visualizza regola',$msgPag,'wfma',$aOpz);
?>
<script type="text/javascript">
<!--
function confElim() {
	if (confirm("Confermi l'eliminazione ?")) {
		document.location.href = "regView.php?<?="id=".reqVarInt('id')."&azione=elimina&$sXSNQS"?>";
	} 
}
function confInst() {
	if (confirm("Confermi l'installazione ?")) {
		document.location.href = "regView.php?<?="id=".reqVarInt('id')."&azione=installa&$sXSNQS"?>";
	} 
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('cod');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Regola</td>
   </tr>
   <tr>
      <td class="lab1" colspan='4' align='left'>Carica da file</td>
      <td class="txt1" colspan='12' align='left'>
      	<?php  echo getHtmlFile('filecar', 60)?>
      	&nbsp;&nbsp;&nbsp;
      	<a href="javascript:document.frm1.azione.value='carica';document.frm1.submit();" class="txt1">carica</a>
		</td>
      <td class="txt1" colspan='4' align='center'><b>
<?php
$pathReg = OVF_FS_WEB_ROOT."/pers/wfma/".$aRow['cod_modulo']
		."/reg".$aRow['tipo_regola']."_".$aRow['cod_regola'].".php";
echo (md5($aRow['script'])==md5_file($pathReg)) ?
	"INSTALLATO" : "<div style='color:red'>NON INSTALLATO</div>";
?>		
		</b></td>
   </tr>
	<?
echo getHtmlCampiForm($aCamForm,'vis');
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function azioneElimina() {
	global $objDB;
	$msg = "";
	$sSql = "SELECT cod_pagina FROM wfm_pagine "
		." WHERE id_regola_val = ".reqVarInt('id')
		." OR id_regola_inp = ".reqVarInt('id')
		." OR id_regola_out = ".reqVarInt('id');
	$objDB->exec($sSql);
	$aX = $objDB->fetch();
	if ($aX) {
		$msg .= "regola utilizzata nella pagina ".$aX['cod_pagina']."<br>";
	}
	if ($msg != "") return $msg;

	$sSql = "DELETE FROM wfm_regole WHERE id_regola = ".reqVarInt('id');
	$objDB->exec($sSql);
	header("location: ".urlTorna("Regola reg'".$aX['tipo_regola']."_".$aX['cod_regola']."' eliminata"));
	exit;
}

function azioneCarica() {
	global $objDB;
	$msg = "";
	if (!is_uploaded_file($_FILES['filecar']['tmp_name']))  {
		return "file non caricato";
	}
	$sql_sel = "UPDATE wfm_regole SET script = '".addslashes(file_get_contents($_FILES['filecar']['tmp_name']))."' "
			." WHERE id_regola = ".reqVarInt('id');
	$objDB->query($sql_sel,'selRec');
	return "regola caricata";
}

function azioneInstalla() {
	global $objDB;
	$msg = "";
	$sSql = "SELECT m.cod_modulo, m.descr as m_descr "
			.", r.* "
			." FROM wfm_regole r " 
			." LEFT JOIN wfm_moduli m ON r.id_modulo = m.id_modulo "
			." WHERE r.id_regola = ".reqVarInt('id');
	$objDB->query($sSql,'selRec');
	if(!$aRow = $objDB->fetch('selRec')) {
		backOnErr("id. errato");
	}
	$pathReg = OVF_FS_WEB_ROOT."/pers/wfma/".$aRow['cod_modulo']
		."/reg".$aRow['tipo_regola']."_".$aRow['cod_regola'].".php";
	file_put_contents($pathReg,$aRow['script']);
	
	return "regola installata";
}
